{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3f12d7c2",
   "metadata": {},
   "source": [
    "# Margins\n",
    "\n",
    " \n",
    "The margins around the plot and text elements are controlled by the `plotMargin` parameter in `theme()` and the `margin` parameter in `elementText()` respectively.\n",
    "\n",
    "Now the parameters `plotMargin` and `margin` accept a number or a list of numbers.\n",
    "\n",
    "- A number or list of one number is specified: the same margin it applied to **all four sides**.\n",
    "- A list of two numbers: the first margin applies to the **top and bottom**, the second - to the **left and right**.\n",
    "- A list of three numbers: the first margin applies to the **top**, the second - to the **right and left**, the third - to the **bottom**.\n",
    "- A list of four numbers: the margins are applied to the **top, right, bottom and left** in that order.\n",
    "\n",
    "It is acceptable to use `null` for any side; in this case, the default side value for this element will be used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9304d8d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"0owRLl\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.2.0/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"0owRLl\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"0owRLl\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1318eeef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lets-Plot Kotlin API v.4.6.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.2.0."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LetsPlot.getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6fb71ad3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import kotlin.random.Random\n",
    "\n",
    "val rand = Random(37)\n",
    "val barData = mapOf(\n",
    "    \"x\" to List(100) { rand.nextInt(10)}\n",
    ")\n",
    "\n",
    "val p = letsPlot(barData) {x = \"x\"} + \n",
    "    geomBar() + \n",
    "    ggtitle(\"Barchart\") +\n",
    "    themeLight() + \n",
    "    theme(plotBackground = elementRect(size = 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "939abcc4",
   "metadata": {},
   "source": [
    "#### Plot without Margins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c3a57af3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"dxVLOi\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Barchart\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"count\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "\"..count..\":[15.0,15.0,8.0,11.0,6.0,9.0,9.0,9.0,10.0,8.0],\n",
       "\"x\":[8.0,4.0,9.0,7.0,3.0,2.0,6.0,5.0,1.0,0.0]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"light\",\n",
       "\"plot_background\":{\n",
       "\"size\":4.0,\n",
       "\"blank\":false\n",
       "}\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"dxVLOi\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12422bb7",
   "metadata": {},
   "source": [
    "#### Margins Around the Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7dda150c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"Sr5KW2\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":2,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"plotMargin=40 (all sides)\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"count\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "\"..count..\":[15.0,15.0,8.0,11.0,6.0,9.0,9.0,9.0,10.0,8.0],\n",
       "\"x\":[8.0,4.0,9.0,7.0,3.0,2.0,6.0,5.0,1.0,0.0]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"light\",\n",
       "\"plot_background\":{\n",
       "\"size\":4.0,\n",
       "\"blank\":false\n",
       "},\n",
       "\"plot_margin\":40.0\n",
       "}\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"plotMargin=[40,20]\\n(top/bottom, left/right)\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"count\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "\"..count..\":[15.0,15.0,8.0,11.0,6.0,9.0,9.0,9.0,10.0,8.0],\n",
       "\"x\":[8.0,4.0,9.0,7.0,3.0,2.0,6.0,5.0,1.0,0.0]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"light\",\n",
       "\"plot_background\":{\n",
       "\"size\":4.0,\n",
       "\"blank\":false\n",
       "},\n",
       "\"plot_margin\":[40.0,20.0]\n",
       "}\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"plotMargin=[40,20,10]\\n(top, left/right, bottom)\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"count\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "\"..count..\":[15.0,15.0,8.0,11.0,6.0,9.0,9.0,9.0,10.0,8.0],\n",
       "\"x\":[8.0,4.0,9.0,7.0,3.0,2.0,6.0,5.0,1.0,0.0]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"light\",\n",
       "\"plot_background\":{\n",
       "\"size\":4.0,\n",
       "\"blank\":false\n",
       "},\n",
       "\"plot_margin\":[40.0,20.0,10.0]\n",
       "}\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"plotMargin=[40,10,10,20]\\n(top, right, bottom, left)\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"count\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "\"..count..\":[15.0,15.0,8.0,11.0,6.0,9.0,9.0,9.0,10.0,8.0],\n",
       "\"x\":[8.0,4.0,9.0,7.0,3.0,2.0,6.0,5.0,1.0,0.0]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"light\",\n",
       "\"plot_background\":{\n",
       "\"size\":4.0,\n",
       "\"blank\":false\n",
       "},\n",
       "\"plot_margin\":[40.0,10.0,10.0,20.0]\n",
       "}\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"Sr5KW2\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(\n",
    "    listOf(\n",
    "        p + theme(plotMargin = 40) + ggtitle(\"plotMargin=40 (all sides)\"),\n",
    "        p + theme(plotMargin = listOf(40, 20)) + \n",
    "            ggtitle(\"plotMargin=[40,20]\\n(top/bottom, left/right)\"),\n",
    "        p + theme(plotMargin = listOf(40, 20, 10)) + \n",
    "            ggtitle(\"plotMargin=[40,20,10]\\n(top, left/right, bottom)\"),\n",
    "        p + theme(plotMargin = listOf(40, 10, 10, 20)) + \n",
    "            ggtitle(\"plotMargin=[40,10,10,20]\\n(top, right, bottom, left)\")\n",
    "    ),\n",
    "    ncol=2\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "789ff5c5",
   "metadata": {},
   "source": [
    "#### Margins Around the Text Element"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1350c45f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"OQGSFC\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"ggtitle\":{\n",
       "\"text\":\"plotTitle=40 -> top/bottom=40,\\naxisTitle=[20,20] -> top/right=20\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"count\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "\"..count..\":[15.0,15.0,8.0,11.0,6.0,9.0,9.0,9.0,10.0,8.0],\n",
       "\"x\":[8.0,4.0,9.0,7.0,3.0,2.0,6.0,5.0,1.0,0.0]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"light\",\n",
       "\"plot_background\":{\n",
       "\"size\":4.0,\n",
       "\"blank\":false\n",
       "},\n",
       "\"plot_title\":{\n",
       "\"margin\":40.0,\n",
       "\"blank\":false\n",
       "},\n",
       "\"axis_title\":{\n",
       "\"margin\":[20.0,20.0],\n",
       "\"blank\":false\n",
       "}\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"OQGSFC\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p + theme(plotTitle = elementText(margin=40),\n",
    "          axisTitle = elementText(margin=listOf(20, 20))) +\n",
    "    ggtitle(\"plotTitle=40 -> top/bottom=40,\\n\" +\n",
    "            \"axisTitle=[20,20] -> top/right=20\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
